#include <head.h>

int main(int argc, char const *argv[])
{
    int fd = open("hello.txt" , O_CREAT |O_RDWR,0666);
    int fd_T = open("T.txt" , O_CREAT |O_RDWR , 0666);

    char buf[32] = "A";
    
    int i = 10;
    int T_num = 0;
    //上锁
    flock(fd , LOCK_EX);
    //通过通讯文件获取光标偏移
    read(fd_T , &T_num ,sizeof(int));

    //移动光标
    lseek(fd , T_num,SEEK_SET);

    while (i--)
    {
        write(fd,buf,1);
        sleep(1);
    }
    
    //写入完文件后再次获取光标偏移准备传给通讯文件
    T_num = lseek(fd , 0 , SEEK_CUR);

    write(fd_T , &T_num,sizeof(int));
    flock(fd , LOCK_UN);

    close(fd);
    close(fd_T);
    return 0;
}
